package it.polimi.swKnights.SWIMv2.filters;

import it.polimi.swKnights.SWIMv2.general.PersistenceManagerUtility;
import it.polimi.swKnights.SWIMv2.general.WebsiteParameters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class SwimFilter
 */
public class SwimFilter implements Filter
{

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException
	{
		((HttpServletResponse) response).sendRedirect("./siteHome");
		//chain.doFilter(request, response);
	}

	protected <T> T lookUp(Class<T> clazz)
	{
		return PersistenceManagerUtility.lookUp(clazz);
	}

	protected String redirectToHomeError()
	{
		return WebsiteParameters.homePageUrl + "errorHome";
	}
	
	protected void setupSessionType(HttpSession session, String wrong, String correct)
	{
		String userType = (String) session.getAttribute("user_type");
		if(userType != null)
		{
			if(userType.equals(wrong))
			{
				session.setAttribute("user_type", correct);
			}
		}
		else
		{
			session.setAttribute("user_type", "guest");
		}
	}

	@Override
	public void destroy()
	{
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException
	{
	}
	
	

}
